package com.ctshk.rpc.order.custom.service.impl;

import com.ctshk.common.enums.StrConstant;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.exception.BusinessException;
import com.ctshk.common.utils.CharUtil;
import com.ctshk.common.utils.DateUtil;
import com.ctshk.rpc.order.custom.service.INumberService;
import com.ctshk.rpc.system.dto.SysDepartmentDTO;
import com.ctshk.rpc.system.dto.UserDTO;
import com.ctshk.rpc.system.service.ISysDepartmentService;
import com.ctshk.rpc.system.service.ISysUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * 单号生成 Service
 */
@DubboService
public class NumberServiceImpl implements INumberService {

    @DubboReference
    private ISysUserService sysUserService;
    @DubboReference
    private ISysDepartmentService sysDepartmentService;


    /**
     * 生成退团单号
     * @param userId
     * @return
     */
    @Override
    public String getQuitNumber(Long userId){
        //退团单号 門市編號+員工編號+年後2位+月日4位+A+時分秒6位+（T）
        String refundNumber = null;
        try {
            UserDTO userDTO = sysUserService.queryCacheUser(userId);
            SysDepartmentDTO departmentDTO = sysDepartmentService.queryOne(userDTO.getDeptId());
            refundNumber = departmentDTO.getDeptCode()+"-"+userDTO.getEmployeeNumber()+"-"+DateUtil.fmtNum(LocalDate.now())+
                    "-"+StrConstant.A.getValue()+"-"+DateUtil.fmt(LocalDateTime.now(),"HHmmss")+"-("+StrConstant.T.getValue()+")";
        }catch (Exception e){
            throw new BusinessException(SystemError.SYS_417);
        }
        return refundNumber;
    }


    /**
     * 生成订单号
     * @param userId
     * @return
     */
    @Override
    public String getOrderNumber(Long userId){
        String orderNo = null;
        try {
            UserDTO userDTO = sysUserService.queryCacheUser(userId);
            if (userDTO != null){
                SysDepartmentDTO departmentDTO = sysDepartmentService.queryOne(userDTO.getDeptId());
                if (departmentDTO != null){
                    //D+部門代碼+員工編號+年後2位+月日4位+3位隨機數字
                    orderNo = StrConstant.D.getValue() + departmentDTO.getDeptCode() + userDTO.getEmployeeNumber() + DateUtil.fmtLocalDate(LocalDate.now(),"yyMMdd") + CharUtil.getRandomStr(3);
                }
            }
        }catch (Exception e){
            throw new BusinessException(SystemError.SYS_417);
        }
        return orderNo;
    }


    /**
     * 获取保险单号
     * @param userId
     * @return
     */
    @Override
    public String getInsuranceNumber(Long userId){
        //保险单号 I+部門代碼+員工編號+年後2位+月日4位+3位隨機數字
        String insuranceNumber = null;
        try {
            UserDTO userDTO = sysUserService.queryCacheUser(userId);
            if (userDTO != null){
                SysDepartmentDTO departmentDTO = sysDepartmentService.queryOne(userDTO.getDeptId());
                if (departmentDTO != null){
                    insuranceNumber = "I-"+departmentDTO.getDeptCode()+"-"+userDTO.getEmployeeNumber()+"-"+
                            DateUtil.fmtLocalDate(LocalDate.now(),"yyMMdd")+"-"+CharUtil.getRandomStr(3);
                }
            }
        }catch (Exception e){
            throw new BusinessException(SystemError.SYS_417);
        }
        return insuranceNumber;
    }


    /**
     * 获取收款单号
     * @return
     */
    @Override
    public String getCollectionNumber(){
        //收款单号 SKD+年後2位+月日4位+時分秒6位+2位隨機數字
        return StrConstant.SKD.getValue() + DateUtil.fmt(LocalDateTime.now(),"yyMMddHHmmss" + CharUtil.getRandomStr(2));
    }


    /**
     * 获取收据单号
     * @param userId
     * @return
     */
    @Override
    public String getReceiptNumber(Long userId){
        String receiptNumber = null;
        try {
            UserDTO userDTO = sysUserService.queryCacheUser(userId);
            if (userDTO != null){
                //收据单号 門市編號+員工編號+年後2位+月日4位+A+時分秒6位+（S）
                receiptNumber = userDTO.getDeptCode()+"-"+userDTO.getEmployeeNumber()+"-"+
                        DateUtil.fmt(LocalDateTime.now(),"yyMMddHHmmss")+"(S)";
            }
        }catch (Exception e){
            throw new BusinessException(SystemError.SYS_417);
        }
        return receiptNumber;
    }

}
